package com.dqhl.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.AddPromotion;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.ViewHolder;

import org.xutils.x;

import java.util.HashMap;
import java.util.List;

/**
 * Created by guanluocang on 2016/11/9.
 */
public class AddPromotionAllGridViewAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private List<AddPromotion> addPromotionList;
    private List<AddPromotion> morePromotionList;
    private HashMap<Integer, Boolean> isSelected;
    //    private boolean isChice[];
//    PromotionMoreDialogActivity mMoreDialogActivity;

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    public AddPromotionAllGridViewAdapter(Context context, List<AddPromotion> addPromotionList,
                                          List<AddPromotion> morePromotionList) {
        this.context = context;
        this.addPromotionList = addPromotionList;
        this.morePromotionList = morePromotionList;
        inflater = LayoutInflater.from(context);
        isSelected = new HashMap<Integer, Boolean>();
        for (int i = 0; i < addPromotionList.size(); i++) {
            getIsSelected().put(i, false);
        }
//        isChice = new boolean[addPromotionList.size()];
//        this.mMoreDialogActivity = mMoreDialogActivity;
//        for (int i = 0; i < addPromotionList.size(); i++) {
//            isChice[i] = false;
//        }
        Dlog.e(addPromotionList.toString());
    }

    @Override
    public int getCount() {
        return addPromotionList.size();
    }

    @Override
    public Object getItem(int i) {
        return addPromotionList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.layout_addpromotion_single, viewGroup, false);
        }
        TextView tv_goods_name = ViewHolder.get(view, R.id.tv_goods_name);
        ImageView iv_promotion = ViewHolder.get(view, R.id.iv_promotion);
        final CheckBox checkbox = ViewHolder.get(view, R.id.checkbox);
        AddPromotion addPromotion = addPromotionList.get(position);

        tv_goods_name.setText(addPromotion.getName());
        x.image().bind(iv_promotion, Config.img_url + addPromotion.getPic_a());

        if (morePromotionList != null && morePromotionList.size() > 0) {
            for (int i = 0; i < morePromotionList.size(); i++) {
                if (addPromotion.getId() == morePromotionList.get(i).getId()) {
                    checkbox.setChecked(true);
                }
            }
            checkbox.setChecked(getIsSelected().get(position));
            if (getIsSelected().get(position)) {
                checkbox.setChecked(true);
                checkbox.setVisibility(View.VISIBLE);
            } else {
                checkbox.setChecked(false);
                checkbox.setVisibility(View.GONE);
            }
            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    isSelected.put(position, true);
                    getIsSelected().put(position, isChecked);
                    checkbox.setChecked(getIsSelected().get(position));
                }
            });
        }

        return view;
    }

}
